﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Linq.Dynamic;
using SDT.Model;
using SDT.Lib.Common;
using SDT.Admin.ViewModels;
using SDT.Lib.Helpers;

namespace SDT.Admin.Controllers
{
	[RoleHelper(Roles = RoleSystem.Dictionary)]
    public class DepartmentController : Controller
    {
		ChoVietDataContext eDataContext = new ChoVietDataContext();
        //
        // GET: /Department/
		public ActionResult Index()
        {
            try
            {
                    var isAdmin = UserManager.CheckRole(RoleSystem.Administrator);
                    int inOrganizationID = UserProfile.GetOrganizationID();
                    string order = (Request["order"] == null || Request["order"] == "") ? "ID" : Request["order"].ToString();
                    string orderDir = (Request["orderDir"] == null || Request["orderDir"] == "") ? "asc" : Request["orderDir"].ToString();
                    int parentID = (Request["parentID"] == null || Request["parentID"] == "") ? 0 : Convert.ToInt32(Request["parentID"]);
                    parentID = isAdmin ? parentID : inOrganizationID;
                    var departments = eDataContext.Organizations.Where(e => e.ParentID != null).Where(e => e.ParentID == parentID).OrderBy(string.Format("{0} {1}", order, orderDir)).ToList();
                    var parentOranization = eDataContext.Organizations.Where(e =>isAdmin ? e.ParentID == null:e.ParentID==inOrganizationID).OrderBy(e => e.Name).ToList();

                    ViewData["orderDir"] = orderDir;
                    ViewData["order"] = order;
                    return View(new DepartmentIndexViewModel
                    {
                        ParrentOrganizations = parentOranization,
                        Departments = departments,
                        CurrentOrganization = parentID
                    });
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }

        //
        // GET: /Department/Details/5
		[Authorize()]
        public ActionResult Details(int id)
        {
            try
            {
                    var item = eDataContext.Organizations.Single(e => e.ID == id);
                    return View(item);
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }

        //
        // GET: /Department/Create
		[Authorize()]
        public ActionResult Create()
        {
            try
            {
                    var isAdmin = UserManager.CheckRole(RoleSystem.Administrator);
                    int inOrganizationID = UserProfile.GetOrganizationID();
                    var parentOranization = eDataContext.Organizations.Where(e => isAdmin ? e.ParentID == null : e.ParentID == inOrganizationID).OrderBy(e => e.Name).ToList();
                    return View(new DepartmentEditViewModel { ParrentOrganizations = parentOranization });
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        } 

        //
        // POST: /Department/Create

        [HttpPost]
        [ValidateInput(false)]
        [Authorize()]
        public ActionResult Create(Organization formData)
        {
            try
            {
                    var item = new Organization();
                    item.Name = formData.Name;
                    item.Code = null;
                    item.ParentID = formData.ParentID;
                    item.Description = formData.Description;
                    item.Modified = DateTime.Now;
                    eDataContext.Organizations.InsertOnSubmit(item);
                    eDataContext.SubmitChanges();
                    return RedirectToAction("Index", new { ParentID = item.ParentID });
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }
        
        //
        // GET: /Department/Edit/5
		[Authorize()]
        public ActionResult Edit(int id)
        {
            try
            {
                    var isAdmin = UserManager.CheckRole(RoleSystem.Administrator);
                    int inOrganizationID = UserProfile.GetOrganizationID();
                    var parentOranization = eDataContext.Organizations.Where(e => isAdmin ? e.ParentID == null : e.ParentID == inOrganizationID).OrderBy(e => e.Name).ToList();
                    var item = eDataContext.Organizations.Single(e => e.ID == id);
                    return View(new DepartmentEditViewModel { ParrentOrganizations = parentOranization, Department = item });
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }

        //
        // POST: /Department/Edit/5

        [HttpPost]
		[ValidateInput(false)]
		[Authorize()]
		public ActionResult Edit(Organization formData)
        {
            try
            {
                    var item = eDataContext.Organizations.Single(e => e.ID == formData.ID);
                    item.ID = formData.ID;
                    item.Code = null;
                    item.ParentID = formData.ParentID;
                    item.Name = formData.Name;
                    item.Description = formData.Description;
                    item.Modified = DateTime.Now;
                    eDataContext.SubmitChanges();
                    return RedirectToAction("Index", new { ParentID = item.ParentID });
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }

        //
        // GET: /Department/Delete/5
		[Authorize()]
        public ActionResult Delete(int id)
        {
            try
            {
                    var item = eDataContext.Organizations.Single(e => e.ID == id);
                    return View(item);
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }

        //
        // POST: /Department/Delete/5

        [HttpPost]
		[Authorize()]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                    var item = eDataContext.Organizations.Single(e => e.ID == id);
                    if (item != null)
                    {
                        eDataContext.Organizations.DeleteOnSubmit(item);
                        eDataContext.SubmitChanges();
                    }
                    return RedirectToAction("Index");
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }
    }
}
